package com.hrm.controller;

import com.hrm.pojo.DTO.NoticePageDTO;
import com.hrm.pojo.Notice;
import com.hrm.service.NoticeService;
import com.hrm.utils.entity.PageResult;
import com.hrm.utils.entity.Result;
import com.hrm.utils.entity.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;



@RequestMapping("/notice")
@RestController
@Api(tags = "公告相关接口")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    /**
     * 公告列表
     * @param noticePageDTO
     * @return
     * @throws Exception
     */
    @GetMapping("/page")
    @ApiOperation("公告列表")
    public Result showList(NoticePageDTO noticePageDTO)throws Exception{
        PageResult<Notice> result=noticeService.pageResult(noticePageDTO);
        return new Result(result!=null,result!=null?StatusCode.SUCCESS:StatusCode.ERROR,"操作完成",result);
    }

    /**
     * 公告修改
     * @param notice
     * @return
     * @throws Exception
     */
    @PutMapping("/update")
    @ApiOperation("公告修改")
    public Result updateNotice(@RequestBody Notice notice)throws Exception{
        boolean flag=noticeService.updateByTitle(notice);
        return new Result(flag,flag ?StatusCode.SUCCESS:StatusCode.ERROR,"操作完成");
    }

    /**
     * 公告删除
     * @param id
     * @return
     * @throws Exception
     */
    @DeleteMapping("/delete/{id}")
    @ApiOperation("公告删除")
    public Result deleteNotice(@PathVariable Integer id)throws Exception{
        boolean flag=noticeService.deleteById(id);
        return new Result(flag,flag ?StatusCode.SUCCESS:StatusCode.ERROR,"操作成功");
    }

    /**
     * 公告增加
     * @param notice
     * @return
     * @throws Exception
     */
    @PostMapping("/add")
    @ApiOperation("增加公告")
    public Result addNotice(@RequestBody Notice notice)throws Exception{
        boolean flag=noticeService.add(notice);
        return new Result(flag,flag ?StatusCode.SUCCESS:StatusCode.ERROR,"操作成功");
    }
}
